/* 
Creates the tables for the analysis of the CUT reform in the amenities sample
	// input: estab_union_act; unionboards_SDSR_clean; filledpanel; estabchars_wide; estabtreat_forRAIS_deid; estabtreat_forRAIS_singest_deid; union_boards_panel_20112019
	// output: 
	    - Table2.xlsx , sheet: table2
		- Table3.xlsx , sheets: T3_mainDD, T3_ratio_versions
		- Table5.xlsx, sheet: table5 
		- TableA8.xlsx, sheets: TA8_mainDD, TA8_ratio_versions
		- TableA9.xlsx, sheets: TA9_mainDD, TA9_ratio_versions
		- TableA10.xlsx, sheets: TA10_mainDD, TA10_ratio_versions
		- TableA11.xlsx, sheet: tableA11
		- TableA12.xlsx, sheet: tableA12
		
*/ 

cap log close
cap log using "$logs/amenities_tables_log", replace


************************
*** SAMPLE DESCRIPTIVES
************************

* data toggles
local CBAbefore = 2020 //2015: must negotiate CBA prior to reform; 2020: ignore restriction
local DDmethod = "base" //base: baseline method; any: clause selected in any method; all: clause selected in all methods
local geoglevel = "state_mode" //state_mode: for state-yr FEs; microregion_mode: for microregion-yr FEs; stateind: for state-industry-yr FEs; microind: for microregion-industry-yr FEs
local no2011 = 1 //0: include 2011; 1: exclude 2011
local clstr = "employer_id" //estab_union_pair; employer_id; union_id
local cov2014 = 0 //0: allow for no CBA coverage in 2014; 1: ensure CBA coverage in 2014 
local filled = 0 //0: use new contracts panel; 1: used filled panel

*** Treatment from establishment sample
	use "$files/estabtreat_forRAIS_deid.dta", clear 
	keep fakeid_estab treat
	rename treat treat_est
	tempfile treat_est
	save `treat_est'
	
*** Sample: establishment-union pairs 
	if `filled' {
		use "$files/filledpanel.dta", clear
	}
	else{
		use "$files/estab_union_act.dta", clear
	}
	keep if ind_mode!=. & microregion_mode!=.
	keep if treat!=. 
	keep if inrange(year, 2011, 2017)	
	keep if min_cbayear<`CBAbefore'
	if `no2011' {
		drop if year==2011
	}
	if `cov2014' {
		gen xxx = (total_clauses==0)&(year==2014)
		egen nocover_bl = max(xxx), by(estab_union_pair)
		unique estab_union_pair
		unique estab_union_pair if nocover_bl==0
		keep if nocover_bl==0
		drop nocover_bl xxx
	}
	gen female_clauses_DD = female_clauses_DD`DDmethod'
	gen male_clauses_DD = male_clauses_DD`DDmethod'
	
	// merge cnpj to establishment ids 
	gen cnpj=subinstr(employer_id, ".", "", .)
	replace cnpj=subinstr(cnpj, "/", "", .)
	replace cnpj=subinstr(cnpj, "-", "", .)
	gen h = length(cnpj)
	replace cnpj = "00"+cnpj if h==12
	drop h 
	destring cnpj, gen(fakeid_estab) 
	format  fakeid_estab %14.0f
	
	//two definitions of treatment for establishments: teart_est vs treatmax
	merge m:1 fakeid_estab using `treat_est', nogen keep(3)
	egen treatmax = max(treat), by(employer_id)
	
	gen order = _n
	
	* Gen columns of table
	gen Var_name = ""
	gen All = . 
	gen Treated =  . 
	gen Control = . 
		
	by estab_union_pair (year), sort: generate N_pairs = _n == 1
	by employer_id (year), sort: generate N_empl = _n == 1
	by union_id (year), sort: generate N_union = _n == 1
	gen N_obs = 1 
	
	
	*** PANEL: Counts
	local tocount N_obs N_pairs N_empl N_union 
	
	sort order 
	local r = 1 
	foreach var of local tocount {
	    
		if "`var'"=="N_empl" {
			replace Var_name = "`var'" in `r' 
			count if `var' == 1 
			replace All = r(N) in `r' 
			count if `var' == 1 & treat_est == 1 
			replace Treated = r(N) in `r'  
			count if `var' == 1 & treat_est == 0
			replace Control = r(N) in `r' 
		}
		else{
			replace Var_name = "`var'" in `r' 
			count if `var' == 1 
			replace All = r(N) in `r' 
			count if `var' == 1 & treat == 1 
			replace Treated = r(N) in `r'  
			count if `var' == 1 & treat == 0
			replace Control = r(N) in `r' 
		}
		
		local ++r
	}
	
		
	bys estab_union_pair: gen temp = _N 
	preserve
		keep estab_union_pair treat temp
		duplicates drop
		su temp
		local avg_years = r(mean)
		
		su temp if treat == 1
		local avg_years_T = r(mean)
		
		su temp if treat == 0
		local avg_years_C = r(mean)
	restore 
	
	replace Var_name = "Years of negotiation" in 5 
	replace All = `avg_years' in 5 
	replace Treated = `avg_years_T' in 5  
	replace Control = `avg_years_C' in 5 
	
	
	*** PANEL: averages
	local toavg total_clauses female_clauses female_clauses_DD male_clauses_DD   
	local r = 7
	foreach var of local toavg {
	    
	    replace Var_name = "`var'" in `r' 
	    su `var' 
		replace All = r(mean) in `r' 
		su `var' if treat == 1 
		replace Treated = r(mean) in `r'  
		su `var' if treat == 0
		replace Control = r(mean) in `r' 
		
		local ++r
	}
		
	keep Var_name All Treated Control 
	keep if All!=. 
	export excel using "$tables/Table2.xlsx", sheet("table2") cell(A1) firstrow(var) sheetreplace 
			
*** Establishment characteristics
	use "$files\estabtreat_forRAIS_singest_deid.dta", clear
	keep fakeid_estab singest 
	duplicates drop
	tempfile singestab 
	save `singestab'
		
	if `filled' {
		use "$files/filledpanel.dta", clear
	}
	else{
		use "$files/estab_union_act.dta", clear
	}
	keep if ind_mode!=. & microregion_mode!=.
	keep if treat!=. 
	keep if inrange(year, 2011, 2017)
	keep if min_cbayear<`CBAbefore'
	if `no2011' {
		drop if year==2011
	}
	if `cov2014' {
		gen xxx = (total_clauses==0)&(year==2014)
		egen nocover_bl = max(xxx), by(estab_union_pair)
		unique estab_union_pair
		unique estab_union_pair if nocover_bl==0
		keep if nocover_bl==0
		drop nocover_bl xxx
	}
	
	// merge cnpj to establishment ids 
	gen cnpj=subinstr(employer_id, ".", "", .)
	replace cnpj=subinstr(cnpj, "/", "", .)
	replace cnpj=subinstr(cnpj, "-", "", .)
	gen h = length(cnpj)
	replace cnpj = "00"+cnpj if h==12
	drop h 
	destring cnpj, gen(fakeid_estab) 
	format  fakeid_estab %14.0f
	
	//two definitions of treatment for establishments: treat_est vs treatmax
	merge m:1 fakeid_estab using `treat_est', nogen keep(3)
	egen treatmax = max(treat), by(employer_id)
	
	* merge with estab characteristics
	merge m:1 employer_id using "$files/estabchars_wide", keepusing(tot_fememployment2014 tot_employment2014)
	drop if _merge == 2
	drop _merge
		
	merge m:1 fakeid_estab using `singestab', keepusing(singest)
	drop if _merge == 2
	replace singest = 0 if singest==.
	drop _merge
	
	replace tot_fememployment2014 = 0 if tot_fememployment2014 == . & tot_employment2014 !=.
	gen sharefemale_2014 = tot_fememployment2014/tot_employment2014
	
	gen tot_malemployment2014 = tot_employment2014 - tot_fememployment2014  if tot_employment2014 !=.
	gen has_both_2014 = (tot_fememployment2014>0 & tot_malemployment2014>0) if tot_employment2014 !=.
		
	* treat_est vs treatmax
	replace treat = treat_est
	keep employer_id treat tot_employment2014  sharefemale_2014 has_both_2014 singest only1union
	duplicates drop
	tab singest has_both_2014
	
	bys employer_id: gen first = _n == 1
	
	
	gen Var_name = ""
	gen All = . 
	gen Treated =  . 
	gen Control = . 
	
	gen order = _n 
	sort order 
	local r = 1 
	
	* PANEL: establishment-level characteristics
	foreach var in tot_employment2014  sharefemale_2014 has_both_2014 singest {
	    
	    replace Var_name = "`var'" in `r' 
	    su `var' if first==1
		replace All = r(mean) in `r' 
		su `var' if treat == 1 & first==1
		replace Treated = r(mean) in `r'  
		su `var' if treat == 0 & first==1
		replace Control = r(mean) in `r' 
		
		local ++r
	}
	
	keep Var_name All Treated Control 
	keep if All!=. 
	export excel using "$tables/Table2.xlsx", sheet("table2") cell(A16)  sheetmodify 
	
	
	
*** Union characteristics  
	if `filled' {
		use "$files/filledpanel.dta", clear
	}
	else{
		use "$files/estab_union_act.dta", clear
	}
	keep if ind_mode!=. & microregion_mode!=.
	keep if treat!=. 
	keep if inrange(year, 2011, 2017)	
	keep if min_cbayear<`CBAbefore'
	if `no2011' {
		drop if year==2011
	}
	if `cov2014' {
		gen xxx = (total_clauses==0)&(year==2014)
		egen nocover_bl = max(xxx), by(estab_union_pair)
		unique estab_union_pair
		unique estab_union_pair if nocover_bl==0
		keep if nocover_bl==0
		drop nocover_bl xxx
	}
	
	keep union_id treat
	duplicates drop 
	tempfile ulist
	save `ulist'
	
	use "$files/unionboards_SDSR_clean.dta", clear 
	keep union_id board_size_bl sh_female_bl female_president_bl female_vicepresident_bl
	duplicates drop
	egen female_PorVP_bl = rowmax(female_president_bl female_vicepresident_bl)
	merge 1:1 union_id using `ulist'
	keep if _merge==3
	drop _merge
	
	gen Var_name = ""
	gen All = . 
	gen Treated =  . 
	gen Control = . 
	
	gen order = _n
	sort order 
	
	local r = 1 
	
	*PANEL: union characteristics 
	foreach var in board_size_bl sh_female_bl female_PorVP_bl {
	    
	    replace Var_name = "`var'" in `r' 
	    su `var' 
		replace All = r(mean) in `r' 
		su `var' if treat == 1 
		replace Treated = r(mean) in `r'  
		su `var' if treat == 0
		replace Control = r(mean) in `r' 
		
		local ++r
	}
	
	keep Var_name All Treated Control 
	keep if All!=. 
	export excel using "$tables/Table2.xlsx", sheet("table2") cell(A22) firstrow(var) sheetmodify 
	

********************************************
*** EFFECT OF REFORM ON NEGOTIATED AMENITIES
********************************************
* baseline

* data toggles
local CBAbefore = 2020 //2015: must negotiate CBA prior to reform; 2020: ignore restriction
local DDmethod = "base" //base: baseline method; any: clause selected in any method; all: clause selected in all methods
local geoglevel = "state_mode" //state_mode: for state-yr FEs; microregion_mode: for microregion-yr FEs; stateind: for state-industry-yr FEs; microind: for microregion-industry-yr FEs
local no2011 = 1 //0: include 2011; 1: exclude 2011
local clstr = "employer_id" //estab_union_pair; employer_id; union_id
local cov2014 = 0 //0: allow for no CBA coverage in 2014; 1: ensure CBA coverage in 2014 
local filled = 1 //0: use new contracts panel; 1: used filled panel

	if `filled' {
		use "$files/filledpanel.dta", clear
	}
	else{
		use "$files/estab_union_act.dta", clear
	}
	keep if ind_mode!=. & microregion_mode!=.
	keep if treat!=. 
	keep if inrange(year, 2011, 2017)	
	keep if min_cbayear<`CBAbefore'
	if `no2011' {
		drop if year==2011
	}
	if `cov2014' {
		gen xxx = (total_clauses==0)&(year==2014)
		egen nocover_bl = max(xxx), by(estab_union_pair)
		unique estab_union_pair
		unique estab_union_pair if nocover_bl==0
		keep if nocover_bl==0
		drop nocover_bl xxx
	}
	
	* data driven method
	gen female_clauses_DD = female_clauses_DD`DDmethod'
	gen male_clauses_DD = male_clauses_DD`DDmethod'
	gen female_clauses_DD_b = female_clauses_DD`DDmethod'_b
	gen male_clauses_DD_b = male_clauses_DD`DDmethod'_b
	gen female_cl_DD_sh = female_cl_DD`DDmethod'_sh
	gen male_cl_DD_sh = male_cl_DD`DDmethod'_sh
	gen female_cl_DD_sh_b = female_cl_DD`DDmethod'_sh_b
	gen male_cl_DD_sh_b = male_cl_DD`DDmethod'_sh_b
	gen any_female_cl_DD = any_female_cl_DD`DDmethod'
	gen any_male_cl_DD = any_male_cl_DD`DDmethod'

	* time varying fixed effects
	egen indyrFE = group(year ind2d_num)
	egen stateind = group(state_mode ind2d_num)
	egen microind = group(microregion_mode ind2d_num)
	egen geogyrFE = group(year `geoglevel')
	if "`geoglevel'"=="stateind" | "`geoglevel'"=="microind" {
		drop indyrFE
	}
	
	* for clustering
	egen clustergrp = group(`clstr')
	
	* gen difference in ratio 
	local shares   female_cl_DD_sh male_cl_DD_sh 
	foreach var of local shares {
		replace `var' = 0 if `var' == . 
	}
	gen FMsh_diff = female_cl_DD_sh - male_cl_DD_sh
	
	local shares   female_cl_DD_sh_b male_cl_DD_sh_b
	foreach var of local shares {
		replace `var' = 0 if `var' == . 
	}
	gen FMsh_diff_b = female_cl_DD_sh_b - male_cl_DD_sh_b
	
	* gen ratio (+1 in denominator)
	gen ratio_DD_V2 = (female_clauses_DD)/(female_clauses_DD+male_clauses_DD+1)
	gen ratio_DD_V2_b = (female_clauses_DD_b)/(female_clauses_DD_b+male_clauses_DD_b+1)
	
	* Panel A: intensive margin
		local depvar female_clauses leave_clauses maternity_clauses harassment_clauses flexibility2_clauses female_clauses_DD male_clauses_DD
		local j=1
			foreach var of local depvar {
				
			reghdfe `var' treatpost , abs(estab_union_pair *FE) cluster(clustergrp)
			estadd scalar obs=e(N_full)
			qui su `var' if year == 2014 & treat == 1
			estadd scalar meandv=r(mean)
			estimates store pa`j'
			
			local ++j
			
				}
				
	* Panel B: intensive margin (binary)
		local depvar female_clauses_b leave_clauses_b maternity_clauses_b harassment_clauses_b flexibility2_clauses_b female_clauses_DD_b male_clauses_DD_b
		local j=1
			foreach var of local depvar {
				
			reghdfe `var' treatpost , abs(estab_union_pair *FE) cluster(clustergrp)
			estadd scalar obs=e(N_full)
			qui su `var' if year == 2014 & treat == 1
			estadd scalar meandv=r(mean)
			estimates store pb`j'
			
			local ++j
			
				}
	
	* Panel C: extensive margin
		local depvar any_female_clauses_cl any_leave_cl any_maternity_cl any_harassment_cl any_flexibility2_cl any_female_cl_DD any_male_cl_DD 
		local j=1
		foreach var of local depvar {
						
			reghdfe `var' treatpost , abs(estab_union_pair *FE) cluster(clustergrp)
			estadd scalar obs=e(N_full)
			qui su `var' if year == 2014 & treat == 1
			estadd scalar meandv=r(mean)
			estimates store pc`j'
			
			local ++j
			
						}
			
	* Panel D: share margin
		local depvar female_sh leave_sh maternity_sh harassment_sh flexibility2_sh female_cl_DD_sh male_cl_DD_sh
		local j=1
		foreach var of local depvar {
		
			replace `var' = 0 if `var' == . 
				
			reghdfe `var' treatpost , abs(estab_union_pair *FE) cluster(clustergrp)
			estadd scalar obs=e(N_full)
			qui su `var' if year == 2014 & treat == 1
			estadd scalar meandv=r(mean)
			estimates store pd`j'
			
			local ++j
			
				}

	
	* RATIOS
	
	local depvar ratio_DD_V2 
		local j=1
			foreach var of local depvar {
				
			reghdfe `var' treatpost , abs(estab_union_pair *FE) cluster(clustergrp)
			estadd scalar obs=e(N_full)
			qui su `var' if year == 2014 & treat == 1
			estadd scalar meandv=r(mean)
			estimates store pe`j'
			
			local ++j
			
			}
	
	*** Export ***
		
	foreach p in a b c d {
		#d ;
		estout 	p`p'1 p`p'2 p`p'3 p`p'4 p`p'5 p`p'6 p`p'7 using "$tables/T3_p`p'.txt", 
		style(tab) mlabels(none) label collabels(none)
		cells(b(star fmt(%9.3f)) se(par)) 
		stats(meandv obs, fmt(%9.2fc %9.0fc ) labels("mean depvar" "N" )) 
		drop(o.*, relax) keep(treatpost, relax) replace starlevels(* 0.10 ** 0.05 *** 0.01);
		#d cr
	}
		
		#d ;
		estout 	pe1 pe2 pe3 pe4 using "$tables/T3_pe.txt", 
		style(tab) mlabels(none) label collabels(none)
		cells(b(star fmt(%9.3f)) se(par)) 
		stats(meandv obs, fmt(%9.2fc %9.0fc ) labels("mean depvar" "N" )) 
		drop(o.*, relax) keep(treatpost, relax) replace starlevels(* 0.10 ** 0.05 *** 0.01);
		#d cr
	
	
	local laba Intensive_margin
	local labb Intensive_margin_binary
	local labc Extensive_margin
	local labd Share
	
	local ra = 1
	local rb = 8 
	local rc = 16 
	local rd = 24 
		
	import delimited "$tables/T3_pa.txt", clear
	rename v1 `laba'
	rename v2 female
	rename v3 leave
	rename v4 maternity
	rename v5 harassment
	rename v6 flexibility
	rename v7 female_DD
	rename v8 male_DD
	export excel using "$tables/Table3.xlsx", sheet("T3_mainDD") cell(A`ra') firstrow(var) sheetreplace
			

	foreach p in b c d {
	
	import delimited "$tables/T3_p`p'.txt", clear
	label var v1 `lab`p''
	label var v2 " "
	label var v3 " "
	label var v4 " "
	label var v5 " "
	label var v6 " "
	label var v7 " "
	label var v8 " " 
			
	export excel using "$tables/Table3.xlsx", sheet("T3_mainDD") cell(A`r`p'') firstrow(varl) sheetmodify

	}
	
	import delimited "$tables/T3_pe.txt", clear
	rename v1 `laba'
	rename v2 new_ratio_1trick
		
	export excel using "$tables/Table3.xlsx", sheet("T3_ratio_versions") cell(A1) firstrow(var) sheetreplace
			

********************************************
*** EFFECT OF REFORM ON NEGOTIATED AMENITIES
********************************************
*use the gendered amenity classification that included industry and state fixed effects

* data toggles
local CBAbefore = 2020 //2015: must negotiate CBA prior to reform; 2020: ignore restriction
local DDmethod = "base" //base: baseline method; any: clause selected in any method; all: clause selected in all methods
local geoglevel = "state_mode" //state_mode: for state-yr FEs; microregion_mode: for microregion-yr FEs; stateind: for state-industry-yr FEs; microind: for microregion-industry-yr FEs
local no2011 = 1 //0: include 2011; 1: exclude 2011
local clstr = "employer_id" //estab_union_pair; employer_id; union_id
local cov2014 = 0 //0: allow for no CBA coverage in 2014; 1: ensure CBA coverage in 2014 
local filled = 1 //0: use new contracts panel; 1: used filled panel


	if `filled' {
		use "$files/filledpanel.dta", clear
	}
	else{
		use "$files/estab_union_act.dta", clear
	}
	keep if ind_mode!=. & microregion_mode!=.
	keep if treat!=. 
	keep if inrange(year, 2011, 2017)	
	keep if min_cbayear<`CBAbefore'
	if `no2011' {
		drop if year==2011
	}
	if `cov2014' {
		gen xxx = (total_clauses==0)&(year==2014)
		egen nocover_bl = max(xxx), by(estab_union_pair)
		unique estab_union_pair
		unique estab_union_pair if nocover_bl==0
		keep if nocover_bl==0
		drop nocover_bl xxx
	}
	
	*use the gendered amenity classification that included industry and state fixed effects
	preserve
		* Baseline: difference in normalized values in the splitsample
		use "$files/amenities_classification_DD_ufind2", clear

		levelsof clause if isfemale_amenity_DD_m2==1, local(female)
		levelsof clause if ismale_amenity_DD_m2==1, local(male)

		local female_am_DD "" 

		foreach x of local female {
			local female_am_DD  "`female_am_DD' `x'" 
		}
		dis "`female_am_DD'"

		local male_am_DD "" 

		foreach x of local male {
			local male_am_DD  "`male_am_DD' `x'" 
		}
		dis "`male_am_DD'"

		//binary

		local female_am_DD_b ""

		foreach x of local female {
			di "`x'"
			local female_am_DD_b  = "`female_am_DD_b' " + "b`x'" 
		}
		dis "`female_am_DD_b'"


		local male_am_DD_b "" 

		foreach x of local male {
			local male_am_DD_b  = "`male_am_DD_b' " + "b`x'" 
		}
		dis "`male_am_DD_b'"
	restore
	
	cap drop *_D*
	egen female_clauses_DDbase = rowtotal(`female_am_DD')
	egen male_clauses_DDbase = rowtotal(`male_am_DD')
	gen any_female_cl_DDbase = (female_clauses_DDbase>0) 
	gen any_male_cl_DDbase = (male_clauses_DDbase>0)  
	gen female_cl_DDbase_sh = female_clauses_DDbase/total_clauses
	gen male_cl_DDbase_sh = male_clauses_DDbase/total_clauses
	egen female_clauses_DDbase_b = rowtotal(`female_am_DD_b')
	egen male_clauses_DDbase_b = rowtotal(`male_am_DD_b')
	gen female_cl_DDbase_sh_b = female_clauses_DDbase_b/total_clauses_b
	gen male_cl_DDbase_sh_b = male_clauses_DDbase_b/total_clauses_b
	replace female_cl_DDbase_sh = 0 if female_cl_DDbase_sh==.
	replace male_cl_DDbase_sh = 0 if male_cl_DDbase_sh==.
	replace female_cl_DDbase_sh_b = 0 if female_cl_DDbase_sh_b==.
	replace male_cl_DDbase_sh_b = 0 if male_cl_DDbase_sh_b==.
	
	* data driven method
	gen female_clauses_DD = female_clauses_DD`DDmethod'
	gen male_clauses_DD = male_clauses_DD`DDmethod'
	gen female_clauses_DD_b = female_clauses_DD`DDmethod'_b
	gen male_clauses_DD_b = male_clauses_DD`DDmethod'_b
	gen female_cl_DD_sh = female_cl_DD`DDmethod'_sh
	gen male_cl_DD_sh = male_cl_DD`DDmethod'_sh
	gen female_cl_DD_sh_b = female_cl_DD`DDmethod'_sh_b
	gen male_cl_DD_sh_b = male_cl_DD`DDmethod'_sh_b
	gen any_female_cl_DD = any_female_cl_DD`DDmethod'
	gen any_male_cl_DD = any_male_cl_DD`DDmethod'

	* time varying fixed effects
	egen indyrFE = group(year ind2d_num)
	egen stateind = group(state_mode ind2d_num)
	egen microind = group(microregion_mode ind2d_num)
	egen geogyrFE = group(year `geoglevel')
	if "`geoglevel'"=="stateind" | "`geoglevel'"=="microind" {
		drop indyrFE
	}
	
	* for clustering
	egen clustergrp = group(`clstr')
	
	* gen difference in ratio 
	local shares   female_cl_DD_sh male_cl_DD_sh 
	foreach var of local shares {
		replace `var' = 0 if `var' == . 
	}
	gen FMsh_diff = female_cl_DD_sh - male_cl_DD_sh
	
	local shares   female_cl_DD_sh_b male_cl_DD_sh_b
	foreach var of local shares {
		replace `var' = 0 if `var' == . 
	}
	gen FMsh_diff_b = female_cl_DD_sh_b - male_cl_DD_sh_b
	
	* gen ratio (+1 in denominator)
	gen ratio_DD_V2 = (female_clauses_DD)/(female_clauses_DD+male_clauses_DD+1)
	gen ratio_DD_V2_b = (female_clauses_DD_b)/(female_clauses_DD_b+male_clauses_DD_b+1)
	
	* Panel A: intensive margin
		local depvar female_clauses leave_clauses maternity_clauses harassment_clauses flexibility2_clauses female_clauses_DD male_clauses_DD
		local j=1
			foreach var of local depvar {
				
			reghdfe `var' treatpost , abs(estab_union_pair *FE) cluster(clustergrp)
			estadd scalar obs=e(N_full)
			qui su `var' if year == 2014 & treat == 1
			estadd scalar meandv=r(mean)
			estimates store pa`j'
			
			local ++j
			
				}
				
	* Panel B: intensive margin (binary)
		local depvar female_clauses_b leave_clauses_b maternity_clauses_b harassment_clauses_b flexibility2_clauses_b female_clauses_DD_b male_clauses_DD_b
		local j=1
			foreach var of local depvar {
				
			reghdfe `var' treatpost , abs(estab_union_pair *FE) cluster(clustergrp)
			estadd scalar obs=e(N_full)
			qui su `var' if year == 2014 & treat == 1
			estadd scalar meandv=r(mean)
			estimates store pb`j'
			
			local ++j
			
				}
	
	* Panel C: extensive margin
		local depvar any_female_clauses_cl any_leave_cl any_maternity_cl any_harassment_cl any_flexibility2_cl any_female_cl_DD any_male_cl_DD 
		local j=1
		foreach var of local depvar {
						
			reghdfe `var' treatpost , abs(estab_union_pair *FE) cluster(clustergrp)
			estadd scalar obs=e(N_full)
			qui su `var' if year == 2014 & treat == 1
			estadd scalar meandv=r(mean)
			estimates store pc`j'
			
			local ++j
			
						}
			
	* Panel D: share 
		local depvar female_sh leave_sh maternity_sh harassment_sh flexibility2_sh female_cl_DD_sh male_cl_DD_sh
		local j=1
		foreach var of local depvar {
		
			replace `var' = 0 if `var' == . 
				
			reghdfe `var' treatpost , abs(estab_union_pair *FE) cluster(clustergrp)
			estadd scalar obs=e(N_full)
			qui su `var' if year == 2014 & treat == 1
			estadd scalar meandv=r(mean)
			estimates store pd`j'
			
			local ++j
			
				}

	* RATIOS
	
	local depvar ratio_DD_V2 
		local j=1
			foreach var of local depvar {
				
			reghdfe `var' treatpost , abs(estab_union_pair *FE) cluster(clustergrp)
			estadd scalar obs=e(N_full)
			qui su `var' if year == 2014 & treat == 1
			estadd scalar meandv=r(mean)
			estimates store pe`j'
			
			local ++j
			
			}
	
	*** Export ***
		
	foreach p in a b c d {
		#d ;
		estout 	p`p'1  p`p'2 p`p'3 p`p'4 p`p'5 p`p'6 p`p'7 using "$tables/T3alt_p`p'.txt", 
		style(tab) mlabels(none) label collabels(none)
		cells(b(star fmt(%9.3f)) se(par)) 
		stats(meandv obs, fmt(%9.2fc %9.0fc ) labels("mean depvar" "N" )) 
		drop(o.*, relax) keep(treatpost, relax) replace starlevels(* 0.10 ** 0.05 *** 0.01);
		#d cr
	}
		
		#d ;
		estout 	pe1 pe2 pe3 pe4 using "$tables/T3alt_pe.txt", 
		style(tab) mlabels(none) label collabels(none)
		cells(b(star fmt(%9.3f)) se(par)) 
		stats(meandv obs, fmt(%9.2fc %9.0fc ) labels("mean depvar" "N" )) 
		drop(o.*, relax) keep(treatpost, relax) replace starlevels(* 0.10 ** 0.05 *** 0.01);
		#d cr
	
	local laba Intensive_margin
	local labb Intensive_margin_binary
	local labc Extensive_margin
	local labd Share
	
	local ra = 1
	local rb = 8 
	local rc = 16 
	local rd = 24 
		
	import delimited "$tables/T3alt_pa.txt", clear
	rename v1 `laba'
	rename v2 female
	rename v3 leave
	rename v4 maternity
	rename v5 harassment
	rename v6 flexibility
	rename v7 female_DD
	rename v8 male_DD
			
	export excel using "$tables/TableA10.xlsx", sheet("TA10_mainDD") cell(A`ra') firstrow(var) sheetreplace

	foreach p in b c d {
	
	import delimited "$tables/T3alt_p`p'.txt", clear
	label var v1 `lab`p''
	label var v2 " "
	label var v3 " "
	label var v4 " "
	label var v5 " "
	label var v6 " "
	label var v7 " "
	label var v8 " " 
	
	export excel using "$tables/TableA10.xlsx", sheet("TA10_mainDD") cell(A`r`p'') firstrow(varl) sheetmodify

	}

	import delimited "$tables/T3alt_pe.txt", clear
	rename v1 `laba'
	rename v2 new_ratio_1trick
		
	export excel using "$tables/TableA10.xlsx", sheet("TA10_ratio_versions") cell(A1) firstrow(var) sheetreplace
			

********************************************
*** EFFECT OF REFORM ON NEGOTIATED AMENITIES
********************************************
*cluster SEs at the union level

* data toggles
local CBAbefore = 2020 //2015: must negotiate CBA prior to reform; 2020: ignore restriction
local DDmethod = "base" //base: baseline method; any: clause selected in any method; all: clause selected in all methods
local geoglevel = "state_mode" //state_mode: for state-yr FEs; microregion_mode: for microregion-yr FEs; stateind: for state-industry-yr FEs; microind: for microregion-industry-yr FEs
local no2011 = 1 //0: include 2011; 1: exclude 2011
local clstr = "union_id" //estab_union_pair; employer_id; union_id
local cov2014 = 0 //0: allow for no CBA coverage in 2014; 1: ensure CBA coverage in 2014 
local filled = 1 //0: use new contracts panel; 1: used filled panel

	if `filled' {
		use "$files/filledpanel.dta", clear
	}
	else{
		use "$files/estab_union_act.dta", clear
	}
	keep if ind_mode!=. & microregion_mode!=.
	keep if treat!=. 
	keep if inrange(year, 2011, 2017)	
	keep if min_cbayear<`CBAbefore'
	if `no2011' {
		drop if year==2011
	}
	if `cov2014' {
		gen xxx = (total_clauses==0)&(year==2014)
		egen nocover_bl = max(xxx), by(estab_union_pair)
		unique estab_union_pair
		unique estab_union_pair if nocover_bl==0
		keep if nocover_bl==0
		drop nocover_bl xxx
	}
	
	* data driven method
	gen female_clauses_DD = female_clauses_DD`DDmethod'
	gen male_clauses_DD = male_clauses_DD`DDmethod'
	gen female_clauses_DD_b = female_clauses_DD`DDmethod'_b
	gen male_clauses_DD_b = male_clauses_DD`DDmethod'_b
	gen female_cl_DD_sh = female_cl_DD`DDmethod'_sh
	gen male_cl_DD_sh = male_cl_DD`DDmethod'_sh
	gen female_cl_DD_sh_b = female_cl_DD`DDmethod'_sh_b
	gen male_cl_DD_sh_b = male_cl_DD`DDmethod'_sh_b
	gen any_female_cl_DD = any_female_cl_DD`DDmethod'
	gen any_male_cl_DD = any_male_cl_DD`DDmethod'

	* time varying fixed effects
	egen indyrFE = group(year ind2d_num)
	egen stateind = group(state_mode ind2d_num)
	egen microind = group(microregion_mode ind2d_num)
	egen geogyrFE = group(year `geoglevel')
	if "`geoglevel'"=="stateind" | "`geoglevel'"=="microind" {
		drop indyrFE
	}
	
	* for clustering
	egen clustergrp = group(`clstr')
	
	* gen difference in ratio 
	local shares   female_cl_DD_sh male_cl_DD_sh 
	foreach var of local shares {
		replace `var' = 0 if `var' == . 
	}
	gen FMsh_diff = female_cl_DD_sh - male_cl_DD_sh
	
	local shares   female_cl_DD_sh_b male_cl_DD_sh_b
	foreach var of local shares {
		replace `var' = 0 if `var' == . 
	}
	gen FMsh_diff_b = female_cl_DD_sh_b - male_cl_DD_sh_b
	
	* gen ratio (+1 in denominator)
	gen ratio_DD_V2 = (female_clauses_DD)/(female_clauses_DD+male_clauses_DD+1)
	gen ratio_DD_V2_b = (female_clauses_DD_b)/(female_clauses_DD_b+male_clauses_DD_b+1)
	
	* Panel A: intensive margin
		local depvar female_clauses leave_clauses maternity_clauses harassment_clauses flexibility2_clauses female_clauses_DD male_clauses_DD
		local j=1
			foreach var of local depvar {
				
			reghdfe `var' treatpost , abs(estab_union_pair *FE) cluster(clustergrp)
			estadd scalar obs=e(N_full)
			qui su `var' if year == 2014 & treat == 1
			estadd scalar meandv=r(mean)
			estimates store pa`j'
			
			local ++j
			
				}
				
	* Panel B: intensive margin (binary)
		local depvar female_clauses_b leave_clauses_b maternity_clauses_b harassment_clauses_b flexibility2_clauses_b female_clauses_DD_b male_clauses_DD_b
		local j=1
			foreach var of local depvar {
				
			reghdfe `var' treatpost , abs(estab_union_pair *FE) cluster(clustergrp)
			estadd scalar obs=e(N_full)
			qui su `var' if year == 2014 & treat == 1
			estadd scalar meandv=r(mean)
			estimates store pb`j'
			
			local ++j
			
				}
	
	* Panel C: extensive margin
		local depvar any_female_clauses_cl any_leave_cl any_maternity_cl any_harassment_cl any_flexibility2_cl any_female_cl_DD any_male_cl_DD
		local j=1
		foreach var of local depvar {
						
			reghdfe `var' treatpost , abs(estab_union_pair *FE) cluster(clustergrp)
			estadd scalar obs=e(N_full)
			qui su `var' if year == 2014 & treat == 1
			estadd scalar meandv=r(mean)
			estimates store pc`j'
			
			local ++j
			
						}
			
	* Panel D: share 
		local depvar female_sh leave_sh maternity_sh harassment_sh flexibility2_sh female_cl_DD_sh male_cl_DD_sh
		local j=1
		foreach var of local depvar {
		
			replace `var' = 0 if `var' == . 
				
			reghdfe `var' treatpost , abs(estab_union_pair *FE) cluster(clustergrp)
			estadd scalar obs=e(N_full)
			qui su `var' if year == 2014 & treat == 1
			estadd scalar meandv=r(mean)
			estimates store pd`j'
			
			local ++j
			
				}

	* RATIOS
	
	local depvar ratio_DD_V2
		local j=1
			foreach var of local depvar {
				
			reghdfe `var' treatpost , abs(estab_union_pair *FE) cluster(clustergrp)
			estadd scalar obs=e(N_full)
			qui su `var' if year == 2014 & treat == 1
			estadd scalar meandv=r(mean)
			estimates store pe`j'
			
			local ++j
			
			}
	
	*** Export ***
		
	foreach p in a b c d {
		#d ;
		estout 	p`p'1  p`p'2 p`p'3 p`p'4 p`p'5 p`p'6 p`p'7 using "$tables/T3_p`p'.txt", 
		style(tab) mlabels(none) label collabels(none)
		cells(b(star fmt(%9.3f)) se(par)) 
		stats(meandv obs, fmt(%9.2fc %9.0fc ) labels("mean depvar" "N" )) 
		drop(o.*, relax) keep(treatpost, relax) replace starlevels(* 0.10 ** 0.05 *** 0.01);
		#d cr
	}
		
		#d ;
		estout 	pe1 pe2 pe3 pe4 using "$tables/T3_pe.txt", 
		style(tab) mlabels(none) label collabels(none)
		cells(b(star fmt(%9.3f)) se(par)) 
		stats(meandv obs, fmt(%9.2fc %9.0fc ) labels("mean depvar" "N" )) 
		drop(o.*, relax) keep(treatpost, relax) replace starlevels(* 0.10 ** 0.05 *** 0.01);
		#d cr
		
	local laba Intensive_margin
	local labb Intensive_margin_binary
	local labc Extensive_margin
	local labd Share
	
	local ra = 1
	local rb = 8 
	local rc = 16 
	local rd = 24 
		
	import delimited "$tables/T3_pa.txt", clear
	rename v1 `laba'
	rename v2 female
	rename v3 leave
	rename v4 maternity
	rename v5 harassment
	rename v6 flexibility
	rename v7 female_DD
	rename v8 male_DD
			
	export excel using "$tables/TableA8.xlsx", sheet("TA8_mainDD") cell(A`ra') firstrow(var) sheetreplace

	foreach p in b c d {
	
	import delimited "$tables/T3_p`p'.txt", clear
	label var v1 `lab`p''
	label var v2 " "
	label var v3 " "
	label var v4 " "
	label var v5 " "
	label var v6 " "
	label var v7 " "
	label var v8 " " 
			
	export excel using "$tables/TableA8.xlsx", sheet("TA8_mainDD") cell(A`r`p'') firstrow(varl) sheetmodify

	}

	import delimited "$tables/T3_pe.txt", clear
	rename v1 `laba'
	rename v2 new_ratio_1trick
		
	export excel using "$tables/TableA8.xlsx", sheet("TA8_ratio_versions") cell(A1) firstrow(var) sheetreplace


********************************************
*** EFFECT OF REFORM ON NEGOTIATED AMENITIES
********************************************
*ensure CBA coverage in 2014 

* data toggles
local CBAbefore = 2020 //2015: must negotiate CBA prior to reform; 2020: ignore restriction
local DDmethod = "base" //base: baseline method; any: clause selected in any method; all: clause selected in all methods
local geoglevel = "state_mode" //state_mode: for state-yr FEs; microregion_mode: for microregion-yr FEs; stateind: for state-industry-yr FEs; microind: for microregion-industry-yr FEs
local no2011 = 1 //0: include 2011; 1: exclude 2011
local clstr = "employer_id" //estab_union_pair; employer_id; union_id
local cov2014 = 1 //0: allow for no CBA coverage in 2014; 1: ensure CBA coverage in 2014 
local filled = 1 //0: use new contracts panel; 1: used filled panel

	if `filled' {
		use "$files/filledpanel.dta", clear
	}
	else{
		use "$files/estab_union_act.dta", clear
	}
	keep if ind_mode!=. & microregion_mode!=.
	keep if treat!=. 
	keep if inrange(year, 2011, 2017)	
	keep if min_cbayear<`CBAbefore'
	if `no2011' {
		drop if year==2011
	}
	if `cov2014' {
		gen xxx = (total_clauses==0)&(year==2014)
		egen nocover_bl = max(xxx), by(estab_union_pair)
		unique estab_union_pair
		unique estab_union_pair if nocover_bl==0
		keep if nocover_bl==0
		drop nocover_bl xxx
	}
	
	* data driven method
	gen female_clauses_DD = female_clauses_DD`DDmethod'
	gen male_clauses_DD = male_clauses_DD`DDmethod'
	gen female_clauses_DD_b = female_clauses_DD`DDmethod'_b
	gen male_clauses_DD_b = male_clauses_DD`DDmethod'_b
	gen female_cl_DD_sh = female_cl_DD`DDmethod'_sh
	gen male_cl_DD_sh = male_cl_DD`DDmethod'_sh
	gen female_cl_DD_sh_b = female_cl_DD`DDmethod'_sh_b
	gen male_cl_DD_sh_b = male_cl_DD`DDmethod'_sh_b
	gen any_female_cl_DD = any_female_cl_DD`DDmethod'
	gen any_male_cl_DD = any_male_cl_DD`DDmethod'

	* time varying fixed effects
	egen indyrFE = group(year ind2d_num)
	egen stateind = group(state_mode ind2d_num)
	egen microind = group(microregion_mode ind2d_num)
	egen geogyrFE = group(year `geoglevel')
	if "`geoglevel'"=="stateind" | "`geoglevel'"=="microind" {
		drop indyrFE
	}
	
	* for clustering
	egen clustergrp = group(`clstr')
	
	* gen difference in ratio 
	local shares   female_cl_DD_sh male_cl_DD_sh 
	foreach var of local shares {
		replace `var' = 0 if `var' == . 
	}
	gen FMsh_diff = female_cl_DD_sh - male_cl_DD_sh
	
	local shares   female_cl_DD_sh_b male_cl_DD_sh_b
	foreach var of local shares {
		replace `var' = 0 if `var' == . 
	}
	gen FMsh_diff_b = female_cl_DD_sh_b - male_cl_DD_sh_b
	
	* gen ratio (+1 in denominator)
	gen ratio_DD_V2 = (female_clauses_DD)/(female_clauses_DD+male_clauses_DD+1)
	gen ratio_DD_V2_b = (female_clauses_DD_b)/(female_clauses_DD_b+male_clauses_DD_b+1)

	
	* Panel A: intensive margin
		local depvar female_clauses leave_clauses maternity_clauses harassment_clauses flexibility2_clauses female_clauses_DD male_clauses_DD 
		local j=1
			foreach var of local depvar {
				
			reghdfe `var' treatpost , abs(estab_union_pair *FE) cluster(clustergrp)
			estadd scalar obs=e(N_full)
			qui su `var' if year == 2014 & treat == 1
			estadd scalar meandv=r(mean)
			estimates store pa`j'
			
			local ++j
			
				}
				
	* Panel B: intensive margin (binary)
		local depvar female_clauses_b leave_clauses_b maternity_clauses_b harassment_clauses_b flexibility2_clauses_b female_clauses_DD_b male_clauses_DD_b 
		local j=1
			foreach var of local depvar {
				
			reghdfe `var' treatpost , abs(estab_union_pair *FE) cluster(clustergrp)
			estadd scalar obs=e(N_full)
			qui su `var' if year == 2014 & treat == 1
			estadd scalar meandv=r(mean)
			estimates store pb`j'
			
			local ++j
			
				}
	
	* Panel C: extensive margin
		local depvar any_female_clauses_cl any_leave_cl any_maternity_cl any_harassment_cl any_flexibility2_cl any_female_cl_DD any_male_cl_DD 
		local j=1
		foreach var of local depvar {
						
			reghdfe `var' treatpost , abs(estab_union_pair *FE) cluster(clustergrp)
			estadd scalar obs=e(N_full)
			qui su `var' if year == 2014 & treat == 1
			estadd scalar meandv=r(mean)
			estimates store pc`j'
			
			local ++j
			
						}
			
	* Panel D: share 
		local depvar female_sh leave_sh maternity_sh harassment_sh flexibility2_sh female_cl_DD_sh male_cl_DD_sh
		local j=1
		foreach var of local depvar {
		
			replace `var' = 0 if `var' == . 
				
			reghdfe `var' treatpost , abs(estab_union_pair *FE) cluster(clustergrp)
			estadd scalar obs=e(N_full)
			qui su `var' if year == 2014 & treat == 1
			estadd scalar meandv=r(mean)
			estimates store pd`j'
			
			local ++j
			
				}

	* RATIOS
	
	local depvar ratio_DD_V2
		local j=1
			foreach var of local depvar {
				
			reghdfe `var' treatpost , abs(estab_union_pair *FE) cluster(clustergrp)
			estadd scalar obs=e(N_full)
			qui su `var' if year == 2014 & treat == 1
			estadd scalar meandv=r(mean)
			estimates store pe`j'
			
			local ++j
			
			}
	
	*** Export ***
		
	foreach p in a b c d {
		#d ;
		estout 	p`p'1  p`p'2 p`p'3 p`p'4 p`p'5 p`p'6 p`p'7 using "$tables/T3_p`p'.txt", 
		style(tab) mlabels(none) label collabels(none)
		cells(b(star fmt(%9.3f)) se(par)) 
		stats(meandv obs, fmt(%9.2fc %9.0fc ) labels("mean depvar" "N" )) 
		drop(o.*, relax) keep(treatpost, relax) replace starlevels(* 0.10 ** 0.05 *** 0.01);
		#d cr
	}
		
		#d ;
		estout 	pe1 pe2 pe3 pe4 using "$tables/T3_pe.txt", 
		style(tab) mlabels(none) label collabels(none)
		cells(b(star fmt(%9.3f)) se(par)) 
		stats(meandv obs, fmt(%9.2fc %9.0fc ) labels("mean depvar" "N" )) 
		drop(o.*, relax) keep(treatpost, relax) replace starlevels(* 0.10 ** 0.05 *** 0.01);
		#d cr
	
	local laba Intensive_margin
	local labb Intensive_margin_binary
	local labc Extensive_margin
	local labd Share
	
	local ra = 1
	local rb = 8 
	local rc = 16 
	local rd = 24 
		
	import delimited "$tables/T3_pa.txt", clear
	rename v1 `laba'
	rename v2 female
	rename v3 leave
	rename v4 maternity
	rename v5 harassment
	rename v6 flexibility
	rename v7 female_DD
	rename v8 male_DD
			
	export excel using "$tables/TableA9.xlsx", sheet("TA9_mainDD") cell(A`ra') firstrow(var) sheetreplace

	foreach p in b c d {
	
	import delimited "$tables/T3_p`p'.txt", clear
	label var v1 `lab`p''
	label var v2 " "
	label var v3 " "
	label var v4 " "
	label var v5 " "
	label var v6 " "
	label var v7 " "
	label var v8 " " 
			
	export excel using "$tables/TableA9.xlsx", sheet("TA9_mainDD") cell(A`r`p'') firstrow(varl) sheetmodify

	}
	
	import delimited "$tables/T3_pe.txt", clear
	rename v1 `laba'
	rename v2 new_ratio_1trick
		
	export excel using "$tables/TableA9.xlsx", sheet("TA9_ratio_versions") cell(A1) firstrow(var) sheetreplace
			

***************************************************
*** EFFECT OF REFORM ON FEMALE AMENITIES: INTENSIVE
***************************************************
*robustness of specification

* data toggles
local CBAbefore = 2020 //2015: must negotiate CBA prior to reform; 2020: ignore restriction
local DDmethod = "base" //base: baseline method; any: clause selected in any method; all: clause selected in all methods
local geoglevel = "state_mode" //state_mode: for state-yr FEs; microregion_mode: for microregion-yr FEs; stateind: for state-industry-yr FEs; microind: for microregion-industry-yr FEs
local no2011 = 1 //0: include 2011; 1: exclude 2011
local clstr = "employer_id" //estab_union_pair; employer_id; union_id
local cov2014 = 0 //0: allow for no CBA coverage in 2014; 1: ensure CBA coverage in 2014 
local filled = 1 //0: use new contracts panel; 1: used filled panel

	if `filled' {
		use "$files/filledpanel.dta", clear
	}
	else{
		use "$files/estab_union_act.dta", clear
	}
	keep if ind_mode!=. & microregion_mode!=.
	keep if treat!=. 
	keep if inrange(year, 2011, 2017)	
	keep if min_cbayear<`CBAbefore'
	if `no2011' {
		drop if year==2011
	}
	
	* data driven method
	gen female_clauses_DD = female_clauses_DD`DDmethod'
	gen male_clauses_DD = male_clauses_DD`DDmethod'
	gen female_clauses_DD_b = female_clauses_DD`DDmethod'_b
	gen male_clauses_DD_b = male_clauses_DD`DDmethod'_b
	gen female_cl_DD_sh = female_cl_DD`DDmethod'_sh
	gen male_cl_DD_sh = male_cl_DD`DDmethod'_sh
	gen female_cl_DD_sh_b = female_cl_DD`DDmethod'_sh_b
	gen male_cl_DD_sh_b = male_cl_DD`DDmethod'_sh_b
	gen any_female_cl_DD = any_female_cl_DD`DDmethod'
	gen any_male_cl_DD = any_male_cl_DD`DDmethod'

	* time varying fixed effects
	egen stateind = group(state_mode ind2d_num)
	egen microind = group(microregion_mode ind2d_num)
	
	* for clustering
	egen clustergrp = group(`clstr')
	
	* gen ratio of female dd to men dd (intensive margin only) 
	gen ratio_DD = female_clauses_DD/(female_clauses_DD+male_clauses_DD)
	gen ratio_DD_b = female_clauses_DD_b/(female_clauses_DD_b+male_clauses_DD_b)
	
	* gen difference in ratio 
	local shares   female_cl_DD_sh male_cl_DD_sh 
	foreach var of local shares {
		replace `var' = 0 if `var' == . 
	}
	gen FMsh_diff = female_cl_DD_sh - male_cl_DD_sh
	
	local shares   female_cl_DD_sh_b male_cl_DD_sh_b
	foreach var of local shares {
		replace `var' = 0 if `var' == . 
	}
	gen FMsh_diff_b = female_cl_DD_sh_b - male_cl_DD_sh_b
		
	* gen ratio (+1 in denominator)
	gen ratio_DD_V2 = (female_clauses_DD)/(female_clauses_DD+male_clauses_DD+1)
	gen ratio_DD_V2_b = (female_clauses_DD_b)/(female_clauses_DD_b+male_clauses_DD_b+1)
	
	* gen ratio (+0.5 in denominator)
	gen ratio_DD_V3 = (female_clauses_DD)/(female_clauses_DD+male_clauses_DD+0.5)
	gen ratio_DD_V3_b = (female_clauses_DD_b)/(female_clauses_DD_b+male_clauses_DD_b+0.5)
	
	* Column (1)-(3): method for data driven classification
	cap drop *FE
	egen indyrFE = group(year ind2d_num)
	egen geogyrFE = group(year state_mode)
		local depvar female_clauses female_clauses_DDbase female_clauses_DDany female_clauses_DDall
		local j=1
			foreach var of local depvar {
				
			reghdfe `var' treatpost , abs(estab_union_pair *FE) cluster(clustergrp)
			estadd scalar obs=e(N_full)
			qui su `var' if year == 2014 & treat == 1
			estadd scalar meandv=r(mean)
			estimates store pa`j'
			
			local ++j
			
				}
				
	* Column (4): microregion-year FEs
	cap drop *FE
	egen indyrFE = group(year ind2d_num)
	egen geogyrFE = group(year microregion_mode)
		local depvar  female_clauses female_clauses_DDbase
		local j=1
			foreach var of local depvar {
				
			reghdfe `var' treatpost , abs(estab_union_pair *FE) cluster(clustergrp)
			estadd scalar obs=e(N_full)
			qui su `var' if year == 2014 & treat == 1
			estadd scalar meandv=r(mean)
			estimates store pb`j'
			
			local ++j
			
				}
				
	* Column (5): microregion-industry-year FEs
	cap drop *FE
	egen geogyrFE = group(year microind)
		local depvar  female_clauses female_clauses_DDbase
			foreach var of local depvar {
				
			reghdfe `var' treatpost , abs(estab_union_pair *FE) cluster(clustergrp)
			estadd scalar obs=e(N_full)
			qui su `var' if year == 2014 & treat == 1
			estadd scalar meandv=r(mean)
			estimates store pb`j'
			
			local ++j
			
				}
	
	* Column (6): CBA coverage in 2014
	cap drop *FE
	egen indyrFE = group(year ind2d_num)
	egen geogyrFE = group(year state_mode)
	gen xxx = (total_clauses==0)&(year==2014)
	egen nocover_bl = max(xxx), by(estab_union_pair)
	unique estab_union_pair
	unique estab_union_pair if nocover_bl==0
	keep if nocover_bl==0
	drop nocover_bl xxx
		local depvar  female_clauses female_clauses_DDbase
		local j=1
			foreach var of local depvar {
				
			reghdfe `var' treatpost , abs(estab_union_pair *FE) cluster(clustergrp)
			estadd scalar obs=e(N_full)
			qui su `var' if year == 2014 & treat == 1
			estadd scalar meandv=r(mean)
			estimates store pc`j'
			
			local ++j
			
				}
	
	*** Export ***
		
	foreach p in a b {
		#d ;
		estout 	p`p'1  p`p'2 p`p'3 p`p'4 using "$tables/T3_p`p'.txt", 
		style(tab) mlabels(none) label collabels(none)
		cells(b(star fmt(%9.3f)) se(par)) 
		stats(meandv obs, fmt(%9.2fc %9.0fc ) labels("mean depvar" "N" )) 
		drop(o.*, relax) keep(treatpost, relax) replace starlevels(* 0.10 ** 0.05 *** 0.01);
		#d cr
	}
	
		#d ;
		estout 	pc1  pc2 using "$tables/T3_pc.txt", 
		style(tab) mlabels(none) label collabels(none)
		cells(b(star fmt(%9.3f)) se(par)) 
		stats(meandv obs, fmt(%9.2fc %9.0fc ) labels("mean depvar" "N" )) 
		drop(o.*, relax) keep(treatpost, relax) replace starlevels(* 0.10 ** 0.05 *** 0.01);
		#d cr	
	
	local laba Cols_1_3
	local labb Cols_4_5
	local labc Cols_6
	
	local ra = 1
	local rb = 8 
	local rc = 16 
		
	import delimited "$tables/T3_pa.txt", clear
	rename v1 `laba'
			
	export excel using "$tables/TableA11.xlsx", sheet("tableA11") cell(A`ra') firstrow(var) sheetreplace

	foreach p in b c {
	
	import delimited "$tables/T3_p`p'.txt", clear
	label var v1 `lab`p'' 
			
	export excel using "$tables/TableA11.xlsx", sheet("tableA11") cell(A`r`p'') firstrow(varl) sheetmodify

	}
				

***********************************************
*** EFFECT OF REFORM ON FEMALE AMENITIES: SHARE
***********************************************
*robustness of specification

* data toggles
local CBAbefore = 2020 //2015: must negotiate CBA prior to reform; 2020: ignore restriction
local DDmethod = "base" //base: baseline method; any: clause selected in any method; all: clause selected in all methods
local geoglevel = "state_mode" //state_mode: for state-yr FEs; microregion_mode: for microregion-yr FEs; stateind: for state-industry-yr FEs; microind: for microregion-industry-yr FEs
local no2011 = 1 //0: include 2011; 1: exclude 2011
local clstr = "employer_id" //estab_union_pair; employer_id; union_id
local cov2014 = 0 //0: allow for no CBA coverage in 2014; 1: ensure CBA coverage in 2014 
local filled = 1 //0: use new contracts panel; 1: used filled panel

	if `filled' {
		use "$files/filledpanel.dta", clear
	}
	else{
		use "$files/estab_union_act.dta", clear
	}
	keep if ind_mode!=. & microregion_mode!=.
	keep if treat!=. 
	keep if inrange(year, 2011, 2017)	
	keep if min_cbayear<`CBAbefore'
	if `no2011' {
		drop if year==2011
	}
	
	* data driven method
	gen female_clauses_DD = female_clauses_DD`DDmethod'
	gen male_clauses_DD = male_clauses_DD`DDmethod'
	gen female_clauses_DD_b = female_clauses_DD`DDmethod'_b
	gen male_clauses_DD_b = male_clauses_DD`DDmethod'_b
	gen female_cl_DD_sh = female_cl_DD`DDmethod'_sh
	gen male_cl_DD_sh = male_cl_DD`DDmethod'_sh
	gen female_cl_DD_sh_b = female_cl_DD`DDmethod'_sh_b
	gen male_cl_DD_sh_b = male_cl_DD`DDmethod'_sh_b
	gen any_female_cl_DD = any_female_cl_DD`DDmethod'
	gen any_male_cl_DD = any_male_cl_DD`DDmethod'

	* time varying fixed effects
	egen stateind = group(state_mode ind2d_num)
	egen microind = group(microregion_mode ind2d_num)
	
	* for clustering
	egen clustergrp = group(`clstr')
	
	* gen ratio of female dd to men dd (intensive margin only) 
	gen ratio_DD = female_clauses_DD/(female_clauses_DD+male_clauses_DD)
	gen ratio_DD_b = female_clauses_DD_b/(female_clauses_DD_b+male_clauses_DD_b)
	
	* gen difference in ratio 
	local shares   female_cl_DD_sh male_cl_DD_sh 
	foreach var of local shares {
		replace `var' = 0 if `var' == . 
	}
	gen FMsh_diff = female_cl_DD_sh - male_cl_DD_sh
	
	local shares   female_cl_DD_sh_b male_cl_DD_sh_b
	foreach var of local shares {
		replace `var' = 0 if `var' == . 
	}
	gen FMsh_diff_b = female_cl_DD_sh_b - male_cl_DD_sh_b
	
	* gen ratio (+1 in denominator)
	gen ratio_DD_V2 = (female_clauses_DD)/(female_clauses_DD+male_clauses_DD+1)
	gen ratio_DD_V2_b = (female_clauses_DD_b)/(female_clauses_DD_b+male_clauses_DD_b+1)
	
	* gen ratio (+0.5 in denominator)
	gen ratio_DD_V3 = (female_clauses_DD)/(female_clauses_DD+male_clauses_DD+0.5)
	gen ratio_DD_V3_b = (female_clauses_DD_b)/(female_clauses_DD_b+male_clauses_DD_b+0.5)
			
	* Column (1)-(3): method for data driven classification
	cap drop *FE
	egen indyrFE = group(year ind2d_num)
	egen geogyrFE = group(year state_mode)
		local depvar  female_sh female_cl_DDbase_sh female_cl_DDany_sh female_cl_DDall_sh
		local j=1
			foreach var of local depvar {
				
			replace `var' = 0 if `var' == . 
				
			reghdfe `var' treatpost , abs(estab_union_pair *FE) cluster(clustergrp)
			estadd scalar obs=e(N_full)
			qui su `var' if year == 2014 & treat == 1
			estadd scalar meandv=r(mean)
			estimates store pa`j'
			
			local ++j
			
				}
				
	* Column (4): microregion-year FEs
	cap drop *FE
	egen indyrFE = group(year ind2d_num)
	egen geogyrFE = group(year microregion_mode)
		local depvar  female_sh female_cl_DDbase_sh
		local j=1
			foreach var of local depvar {
				
			replace `var' = 0 if `var' == . 
				
			reghdfe `var' treatpost , abs(estab_union_pair *FE) cluster(clustergrp)
			estadd scalar obs=e(N_full)
			qui su `var' if year == 2014 & treat == 1
			estadd scalar meandv=r(mean)
			estimates store pb`j'
			
			local ++j
			
				}
				
	* Column (5): microregion-industry-year FEs
	cap drop *FE
	egen geogyrFE = group(year microind)
		local depvar  female_sh female_cl_DDbase_sh
			foreach var of local depvar {
				
			replace `var' = 0 if `var' == . 
				
			reghdfe `var' treatpost , abs(estab_union_pair *FE) cluster(clustergrp)
			estadd scalar obs=e(N_full)
			qui su `var' if year == 2014 & treat == 1
			estadd scalar meandv=r(mean)
			estimates store pb`j'
			
			local ++j
			
				}
	
	* Column (6): CBA coverage in 2014
	cap drop *FE
	egen indyrFE = group(year ind2d_num)
	egen geogyrFE = group(year state_mode)
	gen xxx = (total_clauses==0)&(year==2014)
	egen nocover_bl = max(xxx), by(estab_union_pair)
	unique estab_union_pair
	unique estab_union_pair if nocover_bl==0
	keep if nocover_bl==0
	drop nocover_bl xxx
		local depvar  female_sh female_cl_DDbase_sh
		local j=1
			foreach var of local depvar {

			replace `var' = 0 if `var' == . 
			
			reghdfe `var' treatpost , abs(estab_union_pair *FE) cluster(clustergrp)
			estadd scalar obs=e(N_full)
			qui su `var' if year == 2014 & treat == 1
			estadd scalar meandv=r(mean)
			estimates store pc`j'
			
			local ++j
			
				}	
	
	*** Export ***
		
	foreach p in a b {
		#d ;
		estout 	p`p'1  p`p'2 p`p'3 p`p'4 using "$tables/T3_p`p'.txt", 
		style(tab) mlabels(none) label collabels(none)
		cells(b(star fmt(%9.3f)) se(par)) 
		stats(meandv obs, fmt(%9.2fc %9.0fc ) labels("mean depvar" "N" )) 
		drop(o.*, relax) keep(treatpost, relax) replace starlevels(* 0.10 ** 0.05 *** 0.01);
		#d cr
	}
	
		#d ;
		estout 	pc1  pc2 using "$tables/T3_pc.txt", 
		style(tab) mlabels(none) label collabels(none)
		cells(b(star fmt(%9.3f)) se(par)) 
		stats(meandv obs, fmt(%9.2fc %9.0fc ) labels("mean depvar" "N" )) 
		drop(o.*, relax) keep(treatpost, relax) replace starlevels(* 0.10 ** 0.05 *** 0.01);
		#d cr	
	
	local laba Cols_1_3
	local labb Cols_4_5
	local labc Cols_6
	
	local ra = 1
	local rb = 8 
	local rc = 16 
		
	import delimited "$tables/T3_pa.txt", clear
	rename v1 `laba'
			
	export excel using "$tables/TableA12.xlsx", sheet("tableA12") cell(A`ra') firstrow(var) sheetreplace

	foreach p in b c {
	
	import delimited "$tables/T3_p`p'.txt", clear
	label var v1 `lab`p'' 
			
	export excel using "$tables/TableA12.xlsx", sheet("tableA12") cell(A`r`p'') firstrow(varl) sheetmodify

	}
				

*************************************************
**HETEROGENEITY BY BASELINE FEMALE REPRESENTATION
*************************************************

* data toggles
local CBAbefore = 2020 //2015: must negotiate CBA prior to reform; 2020: ignore restriction
local DDmethod = "base" //base: baseline method; any: clause selected in any method; all: clause selected in all methods
local geoglevel = "state_mode" //state_mode: for state-yr FEs; microregion_mode: for microregion-yr FEs; stateind: for state-industry-yr FEs; microind: for microregion-industry-yr FEs
local no2011 = 1 //0: include 2011; 1: exclude 2011
local clstr = "employer_id" //estab_union_pair; employer_id; union_id
local cov2014 = 0 //0: allow for no CBA coverage in 2014; 1: ensure CBA coverage in 2014 
local filled = 1 //0: use new contracts panel; 1: used filled panel
	
	*** Data prep 
	* Get baseline female share in union boards and a dummy for female Pres/VP
	use "$files/unionboards_SDSR_clean.dta", clear
	keep union_id sh_female_bl female_president_bl female_vicepresident_bl union_type
	//tag unions with info on boards at baseline 
	gen has_bl = (sh_female_bl!=. & female_president_bl!=.)
	duplicates drop
	sum sh_female_bl, d 
	sum sh_female_bl if union_type=="Trabalhador", d 
	gen high_sh_female_bl = (sh_female_bl>1/3) if sh_female_bl!=.
	egen female_PorVP = rowmax( female_president_bl female_vicepresident_bl)
	tab high_sh_female_bl
	tab high_sh_female_bl if union_type=="Trabalhador"
	tab female_PorVP
	tab female_PorVP if union_type=="Trabalhador"
	tempfile baselines
	save `baselines' 
	
	*** Load panel
	if `filled' {
		use "$files/filledpanel.dta", clear
	}
	else{
		use "$files/estab_union_act.dta", clear
	}
	keep if ind_mode!=. & microregion_mode!=.
	keep if treat!=. 
	keep if inrange(year, 2011, 2017)	
	keep if min_cbayear<`CBAbefore'
	if `no2011' {
		drop if year==2011
	}
	if `cov2014' {
		gen xxx = (total_clauses==0)&(year==2014)
		egen nocover_bl = max(xxx), by(estab_union_pair)
		unique estab_union_pair
		unique estab_union_pair if nocover_bl==0
		keep if nocover_bl==0
		drop nocover_bl xxx
	}
	
	* merge with union bl share/bl female pres-VP gender 
	cap drop _merge
	merge m:1 union_id  using `baselines' , keep(1 3)
	drop _merge
	
	* merge with estab characteristics to get share of female employment
	merge m:1 employer_id using "$files/estabchars_wide", keepusing(tot_fememployment2014 tot_employment2014)
	drop if _merge == 2
	drop _merge
	replace tot_fememployment2014 = 0 if tot_fememployment2014 == . & tot_employment2014 !=.
	gen sharefemale_2014 = tot_fememployment2014/tot_employment2014
	egen unique_emp = tag(employer_id)
	sum sharefemale_2014 if unique_emp==1, d 
	local median = r(p50)
	gen high_sharefemale_2014 = (sharefemale_2014>`median') if sharefemale_2014!=.
	//for reference on baseline union characteristics restricted to the estimation sample
	egen unique_uni = tag(union_id)
	sum sh_female_bl if unique_uni==1, d 
	tab high_sh_female_bl if unique_emp==1
	tab female_PorVP if unique_emp==1

	* time varying fixed effects
	egen indyrFE = group(year ind2d_num)
	egen stateind = group(state_mode ind2d_num)
	egen microind = group(microregion_mode ind2d_num)
	egen geogyrFE = group(year `geoglevel')
	if "`geoglevel'"=="stateind" | "`geoglevel'"=="microind" {
		drop indyrFE
	}
	
	* for clustering
	egen clustergrp = group(`clstr')
		
	* SOME COLUMNS ON TABLE WILL BE RESTRICTED TO HAVING INFO ON BOARD AT BASELINE
	tab has_bl
	unique union_id
	unique union_id if has_bl!=1 
	unique estab_union_pair
	unique estab_union_pair if has_bl!=1 
	mdesc high_sh_female_bl female_PorVP high_sharefemale_2014
	
	* replace shares to zero if denominator == 0 
	replace female_sh = 0 if female_sh == . 
	
	* data driven method
	gen female_clauses_DD = female_clauses_DD`DDmethod'
	gen male_clauses_DD = male_clauses_DD`DDmethod'
	gen female_clauses_DD_b = female_clauses_DD`DDmethod'_b
	gen male_clauses_DD_b = male_clauses_DD`DDmethod'_b
	gen female_cl_DD_sh = female_cl_DD`DDmethod'_sh
	gen male_cl_DD_sh = male_cl_DD`DDmethod'_sh
	gen female_cl_DD_sh_b = female_cl_DD`DDmethod'_sh_b
	gen male_cl_DD_sh_b = male_cl_DD`DDmethod'_sh_b
	gen any_female_cl_DD = any_female_cl_DD`DDmethod'
	gen any_male_cl_DD = any_male_cl_DD`DDmethod'
	
	***FEMALE***
	* Columns 1: Main results
	************************************************************

	global depvar female_clauses_DD female_cl_DD_sh

	local j = 1
	foreach var of global depvar {	
			
			reghdfe `var' treatpost , absorb(estab_union_pair *FE) cluster(clustergrp)
			estadd scalar obs=e(N_full)
			estadd scalar sumdum = 0
			estadd scalar sumdump = 0
			qui su `var' if year == 2014 & treat == 1
			estadd scalar meandv=r(mean)
			estimates store a1`j'
												
			local ++j
				
			}
	
	* Column 2 : Low women in establishment
	************************************************************

	global depvar female_clauses_DD female_cl_DD_sh
	gen dummy = (high_sharefemale_2014==0) if high_sharefemale_2014!=.
	
	local j = 1
	foreach var of global depvar {	
			
			reghdfe `var' treatpost##dummy treat##dummy post##dummy, absorb(estab_union_pair *FE) cluster(clustergrp)
			estadd scalar obs=e(N_full)
			qui lincom 1.treatpost + 1.treatpost#1.dummy
			estadd scalar sumdum =r(estimate)
			qui test 1.treatpost + 1.treatpost#1.dummy = 0
			estadd scalar sumdump =r(p)
			qui su `var' if year == 2014 & treat == 1
			estadd scalar meandv=r(mean)
			estimates store b1`j'
												
			local ++j
				
			}
	drop dummy	
	
	
	* Column 3 : Low women in union board
	************************************************************

	global depvar female_clauses_DD female_cl_DD_sh
	gen dummy = (high_sh_female_bl==0) if high_sh_female_bl!=.
	
	local j = 1
	foreach var of global depvar {	
			
			reghdfe `var' treatpost##dummy treat##dummy post##dummy, absorb(estab_union_pair *FE) cluster(clustergrp)
			estadd scalar obs=e(N_full)
			qui lincom 1.treatpost + 1.treatpost#1.dummy
			estadd scalar sumdum =r(estimate)
			qui test 1.treatpost + 1.treatpost#1.dummy = 0
			estadd scalar sumdump =r(p)
			qui su `var' if year == 2014 & treat == 1
			estadd scalar meandv=r(mean)
			estimates store c1`j'
												
			local ++j
				
			}
	drop dummy	
		
	* Columns 4: Not having a female president or VP at baseline 
	*********************************************************************
	global depvar female_clauses_DD female_cl_DD_sh
	gen dummy = (female_PorVP==0) if female_PorVP!=.
	
	local j = 1
	foreach var of global depvar {		
			
			reghdfe `var' treat treatpost##dummy treat##dummy post##dummy, absorb(estab_union_pair *FE) cluster(clustergrp)
			estadd scalar obs=e(N_full)
			qui lincom 1.treatpost + 1.treatpost#1.dummy
			estadd scalar sumdum =r(estimate)
			qui test 1.treatpost + 1.treatpost#1.dummy = 0
			estadd scalar sumdump =r(p)
			qui su `var' if year == 2014 & treat == 1
			estadd scalar meandv=r(mean)
			estimates store d1`j'
												
			local ++j
				
			}
	drop dummy
	
			
	*** Export ***
	forvalues j = 1/2 {
			#d ;
			estout a1`j' b1`j' c1`j' d1`j' using "$tables/T4_p`j'.txt", 
			style(tab) mlabels(none) label collabels(none)
			cells(b(star fmt(%9.3f)) se(par)) 
			stats(sumdum sumdump meandv obs, fmt(%9.3f %9.3f %9.2fc %9.0fc ) labels("sum" "p-value" "mean depvar" "N" )) 
			drop(o.*, relax) keep(treatpost 1.treatpost 1.treatpost#1.dummy, relax) replace starlevels(* 0.10 ** 0.05 *** 0.01);
			#d cr
		}	
					
	local lab1 Intensive_margin
	local lab2 Share
	
	local r1 = 1
	local r2 = 12 
	
	import delimited "$tables/T4_p1.txt", clear
	rename v1 `lab1'
	rename v2 main
	rename v3 low_femwork
	rename v4 low_femunion
	rename v5 no_fempres
			
	export excel using "$tables/Table5.xlsx", sheet("table5") cell(A`r1') firstrow(var) sheetreplace
	
	forvalues p = 2/2 {
	
	import delimited "$tables/T4_p`p'.txt", clear
	label var v1 `lab`p''
	label var v2 " "
	label var v3 " "
	label var v4 " "
	label var v5 " "
			
	export excel using "$tables/Table5.xlsx", sheet("table5") cell(A`r`p'') firstrow(varl) sheetmodify

	}
		

log close
